package condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Profile;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.MultiValueMap;

/**
 * Created by ztt on 2017/9/20.
 */
public class MyProfileCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        if(context.getEnvironment()!=null){
            MultiValueMap<String,Object> arrts=metadata.getAllAnnotationAttributes(Profile.class.getName());
            if(arrts!=null){
                for(Object value:arrts.get("value")){
                    if(context.getEnvironment().acceptsProfiles(((String[])value))){
                        return true;
                    }
                }
                return false;
            }
        }
        return true;
    }
}
